<?php

declare(strict_types=1);

namespace App\Service\Logic;

use App\Service\Business\CustomModuleDataBusiness;
use App\Validator\CommonValidator;
use App\Validator\CustomModuleDataValidator;
use Exception;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * 自定义模块数据-逻辑类
 */
class CustomModuleDataLogic extends BaseLogic
{
        #[Inject]
        protected CustomModuleDataBusiness $CustomModuleDataBusiness;

        #[Inject]
        protected CustomModuleDataValidator $CustomModuleDataValidator;

        #[Inject]
        protected CommonValidator $CommonValidator;

        #[Inject]
        protected CustomModuleDataValueLogic $CustomModuleDataValueLogic;

        /**
         * 分页
         * @return array
         * @throws Exception
         */
        public function paginate(): array
        {
                $this->CommonValidator->validate($this->request->all(), ['page', 'limit']);
                return $this->CustomModuleDataBusiness->paginate();
        }

        /**
         * 创建
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         * @throws Exception
         */
        public function create(): array
        {
                $this->CommonValidator->validate($this->request->all(), ['sort']);
                $this->CustomModuleDataValidator->validate($this->request->all(), ['customModuleId', 'status']);
                $arrResult = $this->CustomModuleDataBusiness->create();
                $this->CustomModuleDataValueLogic->set($arrResult['data']['customModuleDataId']);
                return $arrResult;
        }

        /**
         * 更新
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         * @throws Exception
         */
        public function update(): array
        {
                $this->CommonValidator->validate($this->request->all(), ['sort']);
                $this->CustomModuleDataValidator->validate($this->request->all(), ['customModuleDataId', 'customModuleId', 'status']);
                $arrResult = $this->CustomModuleDataBusiness->update();
                $this->CustomModuleDataValueLogic->set($this->request->input('customModuleDataId'));
                return $arrResult;
        }

        /**
         * 删除
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         * @throws Exception
         */
        public function delete(): array
        {
                $this->CustomModuleDataValidator->validate($this->request->all(), ['customModuleDataId']);
                $arrResult = $this->CustomModuleDataBusiness->delete();
                $this->CustomModuleDataValueLogic->relevanceDelete();
                return $arrResult;
        }

        /**
         * 关联性删除
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         * @throws Exception
         */
        public function relevanceDelete(): array
        {
                $this->CustomModuleDataValidator->validate($this->request->all(), ['customModuleId']);
                return $this->CustomModuleDataBusiness->relevanceDelete($this->request->input('customModuleId'));
        }
}